package com.projectplace.api;

import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import com.projectplace.api.getInfoHandler;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

/*
 * Copyright (c) 2011 Projectplace International AB
 * Permission is hereby granted, free of charge, to any person 
 * obtaining a copy of this software and associated documentation 
 * files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, 
 * publish, distribute, sublicense, and/or sell copies of the Software, 
 * and to permit persons to whom the Software is furnished to do so, 
 * subject to the following conditions:The above copyright notice and this 
 * permission notice shall be included in all copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

public class AsynchronousSender extends Thread {
	
	private HttpRequestBase request;
	private getInfoHandler callBack;
	private DefaultHttpClient client;
	
	protected AsynchronousSender(HttpRequestBase request, getInfoHandler callBack) {
		this.request = request;
		this.callBack = callBack;
		  /* client = new DefaultHttpClient();
		    ClientConnectionManager mgr = client.getConnectionManager();
		    HttpParams params = client.getParams();
		 
		    mgr.closeIdleConnections(500, TimeUnit.MILLISECONDS);
		    mgr.closeExpiredConnections();
		    
		    client = new DefaultHttpClient(new ThreadSafeClientConnManager(params, 
		            mgr.getSchemeRegistry()), params);
		    */
		
			client = new DefaultHttpClient();
		 	HttpParams httpParameters = client.getParams();
		 	ClientConnectionManager mgr = client.getConnectionManager();
		 	
		    HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
		    HttpConnectionParams.setSoTimeout(httpParameters, 5000);
		    ConnManagerParams.setMaxTotalConnections(httpParameters, 20);

		    final ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParameters, mgr.getSchemeRegistry()); 

		    client = new DefaultHttpClient(cm, httpParameters); 
		    final HttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler(3, true);
		    client.setHttpRequestRetryHandler(retryHandler);
	} 
	
	public void run() {
		try {
			final HttpResponse response;	
		
			response = client.execute(request);
			
			int statusOfRequest = response.getStatusLine().getStatusCode();
			
			callBack.onFinish(response, new Error(Integer.toString(statusOfRequest)));
			
		} catch (ClientProtocolException e) {
			client.getConnectionManager().shutdown();
			e.printStackTrace();
		} catch (IOException e) {
			client.getConnectionManager().shutdown();
			e.printStackTrace();
		} 
	}
}

